using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Medianamik.Globalization;
using Medianamik.Globalization.Utilities;
using Medianamik.Tests.Unit.Fakes;
using NUnit.Framework;

namespace Medianamik.Tests.Unit.Globalization.Utilities
{
    [TestFixture]
    public class SqlResourceExporterTest : ResourceExporterTest<StringBuilder>
    {
        
        private SqlResourceExporter _exporter;
        
        protected override ResourceExporter<StringBuilder> GetExporter()
        {
            return _exporter;
        }

        protected override int ResourceCount
        {
            get
            {
                
                var sb = _exporter.Export(new[]{_firstResourceSetId }, _manager);

                var resourceCount = sb.ToString()
                    .Split(new []{"go\n"},  StringSplitOptions.RemoveEmptyEntries)
                    .Count();

                return resourceCount;
            }
        }

        [SetUp]
        public void Init()
        {
            _exporter = new SqlResourceExporter();
            _manager = new FakeMedianamikResourceDataManager();     
            AddResources();
            _firstResourceSetId = _resources.First().ResourceSet;
        }

        [Test]
        public void Exporter_contains_all_data_of_the_resources()
        {
            foreach (var resource in _resources)
                _manager.UpdateOrAdd(resource);

            
            var actualSql = _exporter.Export(new[] { _firstResourceSetId }, _manager).ToString();
            var actualQueries = actualSql.Split(new[] {"go\n"}, StringSplitOptions.RemoveEmptyEntries);

            var expectedCount = _resources.Where(x => x.ResourceSet == _firstResourceSetId).Count();
            Assert.That(actualQueries.Count(), Is.EqualTo(expectedCount));

            for (int i = 0; i < actualQueries.Count();i++)
            {
                Assert.That(actualQueries[i].Contains(_resources[i].Id));
                Assert.That(actualQueries[i].Contains(_resources[i].CultureName));
                Assert.That(actualQueries[i].Contains(_resources[i].Value));
                Assert.That(actualQueries[i].Contains(_resources[i].ResourceSet));
            }
        }
        
        [Test]
        public void Exporter_size_should_be_equal_to_0_when_no_resource_is_available()
        {
            var sb = _exporter.Export(new[] { _firstResourceSetId }, _manager);

            Assert.That(sb.ToString().Length, Is.EqualTo(0));
        }
        [Test]
        public void Export_should_add_quote_when_single_quotes_are_found_in_value()
        {
            var resourceSetId = "resourceSet";
            var valueBeforeExport = "ajouter l'image";
            var resource = new Resource("id", "fr-CA", valueBeforeExport, resourceSetId);
            _manager.UpdateOrAdd(resource);

            var expectedValue = "ajouter l''image";

            var actualSql = _exporter.Export(new[] { resourceSetId }, _manager).ToString();

            Assert.That(actualSql.Contains(expectedValue), Is.EqualTo(true), "expectedValue was not found");
            Assert.That(actualSql.Contains(valueBeforeExport), Is.EqualTo(false), "value before export was found");
        }
    }
}